<template>
  <div>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="投诉类型" prop="complaintType">
            <el-select v-model="queryParams.complaintType" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in complaintTypeOptions" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="complaintList" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="姓名" prop="staffName"/>
      <el-table-column align="center" label="电话" prop="staffPhone"/>
      <el-table-column align="center" label="职务" prop="projectDuitsName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="投诉类型" prop="complaintType">
        <template slot-scope="scope">
          <dict-tag :options="complaintTypeOptions" :value="scope.row.complaintType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否匿名" prop="isAnonymous">
        <template slot-scope="scope">
          {{ scope.row.isAnonymous == 0 ? '否' : '是' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="投诉对象" prop="complaintStaffName"/>
      <el-table-column align="center" label="投诉对象职务" prop="complaintStaffDuitsName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.complaintStaffDuitsName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="投诉内容" prop="complaintContent"/>
      <el-table-column align="center" label="投诉时间" prop="complaintTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.complaintTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否回复" prop="isReply">
        <template slot-scope="scope">
          {{ scope.row.isReply == 0 ? '' : '已回复' }}
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:staffComplaint:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row, 1)">回复
          </el-button>
          <el-button v-hasPermi="['cld:staffComplaint:view']" size="mini" type="text"
                     @click="handleUpdate(scope.row, 2)">查看
          </el-button>
          <el-button v-hasPermi="['cld:staffComplaint:remove']" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改员工投诉对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="投诉记录" width="500px">
      <el-form ref="form" :model="form" :rules="rules">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ form.staffName }}({{
                selectDictLabel(dict.type.cld_project_post, form.projectPostName)
              }})</span>
            <span style="float: right; padding: 3px 0" type="text">{{ form.complaintTime }}</span>
          </div>
          <div>
            {{ form.complaintContent }}
          </div>
        </el-card>
        <el-card v-for="item in form.detailsList" :key="item" class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ item.nickName }}(管理员)</span>
            <span style="float: right; padding: 3px 0" type="text">{{ item.replyTime }}</span>
          </div>
          <div>
            {{ item.replyContent }}
          </div>
        </el-card>
        <el-form-item v-if="type == 1" prop="replyContent" style="margin-top: 10px;">
          <el-input v-model="form.replyContent" :rows="4" placeholder="请输入内容" type="textarea">
          </el-input>
        </el-form-item>
      </el-form>
      <div v-if="type == 1" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addReply, delComplaint, getComplaint, listComplaint} from "@/api/cld/degreetitle";
import {complaintTypeOptions} from '@/assets/staticDict'

export default {
  name: "Complaint",
  dicts: ['cld_project_duits', 'cld_project_region', 'cld_project_post'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工投诉表格数据
      complaintList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        complaintType: null,
        projectRegion: null,
        projectName: null
      },
      // 表单参数
      form: {},
      complaintTypeOptions: complaintTypeOptions,
      type: 1
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工投诉列表 */
    getList() {
      this.loading = true;
      listComplaint(this.queryParams).then(response => {
        this.complaintList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        staffComplaintId: null,
        projectId: null,
        staffId: null,
        complaintType: null,
        complaintStaffId: null,
        complaintContent: null,
        complaintTime: null,
        isAnonymous: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.reset();
      this.type = type;
      const staffComplaintId = row.staffComplaintId || this.ids
      getComplaint(staffComplaintId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工投诉";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addReply(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const staffComplaintIds = row.staffComplaintId || this.ids;
      this.$modal.confirm('是否确认删除员工投诉编号为"' + staffComplaintIds + '"的数据项？').then(function () {
        return delComplaint(staffComplaintIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
